package cn.shop.core.controller.front;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import cn.shop.core.bean.GoodsInfo;
import cn.shop.core.bean.UserInfo;
import cn.shop.core.service.GoodsService;
import cn.shop.core.service.UserService;
import cn.shop.utils.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.sun.net.httpserver.HttpsConfigurator;


/**
 * 前台商品查询
 * 
 * @author Administrator
 *
 */
@Service
@RequestMapping("/front/")
public class FrontGoodsController {

	@Autowired
	private GoodsService service;
	
	@Autowired
	private UserService userService;
	
	/**
	 * 初始化首页信息
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("list.shtml")
	public String list(Model model) {

		GoodsInfo info = new GoodsInfo();
		info.setGoodsState(Common.GOODS_STATE_YES);

		List<Map> list = service.getGoodsList(info);
		System.out.println("-------:" + list.size());
		model.addAttribute("goodslist", list);

		return "index";
	}

	/**
	 * 查看商品
	 * 
	 * @param goodsId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "{goodsId}.shtml", method = RequestMethod.GET)
	public String getinfo(@PathVariable Integer goodsId, Model model) {

		System.out.println("商品编号：" + goodsId);

		GoodsInfo info = new GoodsInfo();
		info.setGoodsId(goodsId);
		Map pinfo = service.getGoods(info);
		if (pinfo != null) {
			model.addAttribute("goods", pinfo);

			return "goodsinfo";
		} else {
			return "error";
		}
	}
	
	@RequestMapping("loadreg.shtml")
	public String loadReg(){
		return "reg";
	}
	@RequestMapping("loadlogin.shtml")
	public String loadLogin(){
		return "login";
	}
	
	@RequestMapping("reg.shtml")
	public String reg(UserInfo user, Model model){
		
		try {
			if(user!=null){
				//普通用户
				user.setUserType(Common.USER_TYPE_ONE);
			}
			
			userService.addUser(user);
			model.addAttribute("info", "恭喜您，用户注册成功!");
			return "login";
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "reg";
	}
	
	
	/**
	 * 用户登录
	 * @param user
	 * @param session
	 * @return
	 */
	@RequestMapping("login.shtml")
	public String login(UserInfo user,HttpSession session,Model model){
		UserInfo userInfo = userService.userLogin(user);
		if(userInfo!=null){
			session.setAttribute("userInfo", userInfo);
			return list(model);
		}else{
			model.addAttribute("logininfo", "账号密码错误，请重新输入");
			return "login";
		}
	}
	
	/**
	 * 购物车
	 * @param goodsId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "car/{goodsId}.shtml", method = RequestMethod.GET)
	public String shopcar(@PathVariable Integer goodsId, Model model,HttpSession session) {

		Map<Integer,Map> cars =  (Map<Integer,Map>)session.getAttribute("cars");
		
		if(cars==null){
			 cars = new HashMap<Integer,Map>();
		}
		
		GoodsInfo info = new GoodsInfo();
		info.setGoodsId(goodsId);
		Map pinfo = service.getGoods(info);
		cars.put(goodsId, pinfo);
		
		session.setAttribute("cars", cars);
		
		return "shoppingcart";
	}
	
	/**
	 * 查看购物车
	 * @return
	 */
	@RequestMapping("showcart.shtml")
	public String showcart(HttpSession session){

		return "shoppingcart";
	}
	
	@RequestMapping("deleteGoods.shtml")
	public String deleteGoods(HttpSession session,Integer[] goodsIds){
		Map<Integer,Map> cars =  (Map<Integer,Map>)session.getAttribute("cars");
		
		if(goodsIds!=null){
			for(Integer goodsid:goodsIds){
				cars.remove(goodsid);
				
			}
			
		}
		
		session.setAttribute("cars", cars);
		
		return "shoppingcart";
	}
}
